<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container" style="display: none;">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" >
        <el-form-item label="名称" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请输入名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button class="filter-item" type="primary" icon="search" @click="handleQuery">
            搜索
          </el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  
    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <!-- <div class="data-table__toolbar--actions">
          <el-button
            v-hasPerm="['finance:commission:add']"
            type="success"
            icon="plus"
            @click="handleOpenDialog()"
          >
            新增
          </el-button>
          <el-button
            v-hasPerm="['finance:commission:delete']"
            type="danger"
            :disabled="selectIds.length === 0"
            icon="delete"
            @click="handleDelete()"
          >
            删除
          </el-button>
        </div> -->
      </div>

      <el-table
        v-loading="loading"
        :data="Lists"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        class="data-table__content"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" label="序号" width="100" />
        <el-table-column prop="chargeFee" label="提现手续费" />
        <el-table-column prop="riseU" label="兑换USDT比例" />
        <el-table-column prop="tronAddress" label="波场地址" />
        <el-table-column prop="bnbAddress" label="币安地址" />
        <!-- <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column prop="sort" label="排序"  />
         <el-table-column prop="status" label="状态" >
          <template #default="scope">
            <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column> -->

        <el-table-column label="操作" fixed="right" align="left" width="200">
          <template #default="scope">
            
            <el-button
              v-hasPerm="['finance:commission:edit']"
              type="primary"
              link
              size="small"
              icon="edit"
              @click.stop="handleOpenDialog(scope.row.id,scope.row)"
            >
              编辑
            </el-button>
            <!-- <el-button
              v-hasPerm="['finance:commission:delete']"
              type="danger"
              link
              size="small"
              icon="delete"
              @click.stop="handleDelete(scope.row.id)"
            >
              删除
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="600px"
      @closed="handleCloseDialog"
    >
      <el-form ref="FormRef" :model="formData" :rules="rules" label-width="120px">
        
        <el-form-item label="提现手续费" prop="name">
          <el-input v-model="formData.chargeFee" placeholder="请输入提现手续费" />
        </el-form-item>

        <el-form-item label="兑换USDT比例">
          <el-input v-model="formData.riseU" placeholder="请输入兑换USDT比例" />
        </el-form-item>

        <el-form-item label="波场地址">
          <el-input v-model="formData.tronAddress" placeholder="请输入波场地址" />
        </el-form-item>

        <el-form-item label="币安地址">
          <el-input v-model="formData.bnbAddress" placeholder="请输入币安地址" />
        </el-form-item>
        
        <!-- <el-form-item label="显示排序" prop="sort">
          <el-input-number
            v-model="formData.sort"
            controls-position="right"
            style="width: 100px"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="状态" >
          <el-radio-group v-model="formData.status">
            <el-radio :value="1">正常</el-radio>
            <el-radio :value="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "手续费",
  inheritAttrs: false,
});

import FinanceAPI, { CommissionVO, CommissionForm } from "@/api/system/finance.api";

const queryFormRef = ref();
const FormRef = ref();

const loading = ref(false);
const selectIds = ref<number[]>([]);
const queryParams = reactive<any>({});

const dialog = reactive({
  title: "",
  visible: false,
});

const Lists = ref();
const formData = reactive<CommissionForm>({
  id: undefined,
  chargeFee: "",
  riseU: "",
  sort: 1,
  status: 1,
  tronAddress: "",
  bnbAddress: "",
});

const rules = reactive({
  chargeFee: [
    { required: true, message: "请输入提现手续费", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  riseU: [
    { required: true, message: "请输入兑换USDT比例", trigger: "blur" },
    { pattern: /^[0-9]+(\.[0-9]{1,2})?$/, message: "请输入正确的格式", trigger: "blur" },
  ],
});

// 查询
function handleQuery() {
  console.log(queryParams)
  loading.value = true;

  FinanceAPI.getCommissionList()
    .then((res) => {
      Lists.value = res;
      console.log("查询结果:", Lists);
    })
    .catch((error) => {
      console.error("查询失败:", error);
      ElMessage.error("查询失败，请稍后重试");
    })
    .finally(() => {
      loading.value = false;
    });

 
  loading.value = false;
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  handleQuery();
}

// 处理选中项变化
function handleSelectionChange(selection: any) {
  selectIds.value = selection.map((item: any) => item.id);
}


const editId = ref();

/**
 * 打开新增弹窗
 *
 * @param Id ID
 */
async function handleOpenDialog(Id?:string,data?:CommissionVO) {

  dialog.visible = true;
  if (Id) {
    dialog.title = "修改";
    editId.value = Id;
    Object.assign(formData, data);
  } else {
    dialog.title = "新增";
  }
}

// 提交表单
function handleSubmit() {
  FormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      
      if (formData.id) {
        const json ={
          "chargeFee":formData.chargeFee, //提现手续费
          "rise": formData.riseU, //积分兑USDT比例
          "tronAddress": formData.tronAddress, //波场地址
          "bnbAddress": formData.bnbAddress, //币安地址
        }
        FinanceAPI.update(json)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleQuery();
          })
          .finally(() => (loading.value = false));
      }     
    }
  });
}


// 重置表单
function resetForm() {
  FormRef.value.resetFields();
  FormRef.value.clearValidate();

  formData.id = undefined;
  formData.status = 1;
  formData.sort = 1;
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  resetForm();
}

onMounted(() => {
  handleQuery();
});
</script>
